﻿public class NetHandler_DetailInfo : GameServerMessageHandler
{
    public const string Key = "UploadDetailInfo";
    //public static object baton = new object();

    public static NetHandler_DetailInfo NewHandler()
    {
        return new NetHandler_DetailInfo();
    }

    protected override ISerialize NewData() => new DetailInfoRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        DetailInfoRequestData requestData = _data as DetailInfoRequestData;

        GameDebug.Log($"用户{client.userID} 上传信息:{requestData?.userDetailInfoData}");

        UserServer user = Game.instance.GetUser(client.userID);

        ImageUrlResponseData response = new ImageUrlResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            if (requestData != null)
            {
                UserDetailInfoData infoData = requestData.userDetailInfoData;

                user.data.UpdateDetailInfo(infoData);

                if (!Tex.IsNull(infoData.icon))
                {
                    int texId = 1;
                    Tex texData = requestData.userDetailInfoData.icon;
                    int setTextRet = TextureSaver.SaveUserTexture(client.userID, texId, texData.texWidth, texData.texHeight, texData.tex);
                    
                    response.url = TextureSaver.GetUserTextureUrl(client.userID, texId);

                    GameDebug.Log($" 更新texture结果：setTextRet:{setTextRet}");
                }
                int ret = users.UpdateDetailInfo(client.userID, infoData.height, infoData.weight, 
                    infoData.maritalStatus, infoData.hometownProvince,infoData.hometownCity,infoData.job);

                GameDebug.Log($" 更新DetailInfo结果：{ret} height:{user.data.userInfo.gender}");
                if (ret >= 0)
                    response.code = 0;
                else
                    response.code = -1;
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}

